package es.ats.codington.festival.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorService;

public class PresentUpdateProfileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/updateProfile.jsp");
        requestDispatcher.forward(request, response);
		
        
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession sesion = request.getSession(false);
		if (sesion == null){
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("/");
	        requestDispatcher.forward(request, response);
		}
		else{
			
			int peopleId = (Integer) sesion.getAttribute("peopleid");
			String firstname = request.getParameter("firstname");
			String lastname = request.getParameter("lastname");
			String nif = request.getParameter("nif");
			String email = request.getParameter("email");
			String phone = request.getParameter("phone");
	//		String address = request.getParameter("address");
	//		String username = request.getParameter("username");
			Visitor visitor = new Visitor();
			VisitorService vs = new VisitorService();
			
			try {
			
				
				
				
	
				visitor.setPeopleId(peopleId);
				visitor.setFirstName(firstname);
				visitor.setLastName(lastname);
				visitor.setNif(nif);
				visitor.setEmail(email);
				visitor.setPhoneNumber(phone);
				
				
							
				int a=vs.updateVisitorFirstNameById(visitor);
				int b=vs.updateVisitorLastNameById(visitor);
				int c=vs.updateVisitorNifById(visitor);
				int d=vs.updateVisitorEmailAddressById(visitor);
				int e=vs.updateVisitorPhoneNumberById(visitor);
				
				if(a==0||b==0||c==0||d==0||e==0)
				{
					request.setAttribute("error", "There was a problem while change some or any datas of your profile. Please try change them again");
					response.sendRedirect("jsp/error.jsp");	
					
					
				}
				else
				{
					visitor=vs.getVisitorById(peopleId);
					if(visitor.getAdmin())
					{
						RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/home.jsp");
				        requestDispatcher.forward(request, response);
					}
					else
					{
						RequestDispatcher requestDispatcher = request.getRequestDispatcher("jsp/home.jsp");
				        requestDispatcher.forward(request, response);
					}	
					
					
				}

			
			} 	
			catch (Exception e) {
				request.setAttribute("error", "Something were wrong with the change of password. Please, try it again in a few minutes.");
				response.sendRedirect("jsp/error.jsp");			
			}
		}
	}
}
